import Vue from 'vue'
import VueRouter from 'vue-router'
// 引入组件路径
import Login from '../components/Login.vue'
import Home from '../components/Home'

Vue.use(VueRouter)


const router = new VueRouter({
  // 将路由添加到相应的路由规则中
  routes: [
    // 路由重定向
    {
      path: '/',
      redirect: '/login'
    }, {
      path: '/login',
      component: Login,
    }, {
      path: '/home',
      component: Home
    }
  ]
})

router.beforeEach((to, from, next) => {
  // to 将要去的页面
  // from从哪里跳转来的
  // next 放行 next()强制跳转到某页面
  if (to.path === '/login') next()
  // 获取token值
  const token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()

})

export default router
